<!--带搜索按钮的输入框组件,需要在插槽中自定义按钮-->
<template>
  <a-form-item :label="label">
    <a-input-search
        :style="getStyle"
        :placeholder="!isDisabled?cplaceholder:''"
        :disabled="isDisabled"
        :readOnly="true"
        @search="search"
        v-decorator="vDecoratorText"
        :class="{'c-form-disabled':isDisabled,'c-form-input':true}"
    >
      <slot></slot>
    </a-input-search>
  </a-form-item>
</template>

<script>
import './css/form.less'
import { mixin } from './js/form.js'

export default {
  name: 'AntFormInputSearch',
  mixins: [mixin],
  data() {
    return {
      //是否禁用和输入提示，由AntForm组件调用
      cdisabled: false,
      cplaceholder: this.placeholder ? this.placeholder : '请输入' + this.label,
      vDecoratorText: this.getDecoratorText()
    }
  },
  props: {
    /**
     * 是否禁用
     */
    disabled: {
      type: Boolean,
      default: false
    },
    /**
     * 是否由form表单自动布局
     */
    autoLayoutByForm: {
      type: Boolean,
      default: true
    },
    /**
     * 显示文本
     */
    label: {
      type: String,
      default: ''
    },
    /**
     * 输入提示
     */
    placeholder: {
      type: String,
      default: ''
    },
    /**
     * 绑定字段 ['phone' ,{ rules: [{ required: true},{validator:function(rule, value, callback)] }],自定义验证方法时callback回调函数必须在方法中调用，
     * 失败返回错误提示callback("xxx")，成功返回callback()
     */
    vDecorator: {
      type: Array,
      default: () => []
    },
    /**
     * css样式，优先级最高
     */
    cstyle: {
      type: String,
      default: ''
    },
    /**
     * 非自动布局的宽度，优先级最低
     */
    defaultWidth: {
      type: Number,
      default: 240
    }
  },
  computed: {
    /**
     * 获取样式
     */
    getStyle() {
      return { width: (this.percentageLayoutWidth ? this.percentageLayoutWidth : this.defaultWidth) + 'px', ...this.cstyle }
    }
  },
  created() {
  },
  mounted() {

  },
  watch: {},
  beforeDestroy() {
  },
  methods: {
    /**
     * 触发搜索
     * @param value
     * @param event
     */
    search(value, event) {
      this.$emit('search', value, event)
    },
    /**
     * 获取绑定值
     * @returns {any}
     */
    getDecoratorText() {
      let vDecoratorText = JSON.parse(JSON.stringify(this.vDecorator))
      if (vDecoratorText.length > 0) {
        vDecoratorText[0] = vDecoratorText[0] + 'Text'
      }
      return vDecoratorText
    },
    /**
     * 赋值之前的转换操作,由AntForm组件自动调用
     * 参数：recordPara，转换之前的record
     * 返回值：转换之后的可用于赋值的对象
     */
    beforeSetValue(recordPara) {
      let record = {}
      let key = this.vDecorator[0]
      record[key] = recordPara[key]
      record[key + 'Text'] = recordPara[key + 'Text']
      return record
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .ant-input:hover, ::v-deep .ant-input:focus {
  border-color: @primary-color;
}
</style>
